package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.enums.DictionaryType;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.utils.ExcelUtil;
import com.zjuee.core.utils.ExportExcel;
import com.zjuee.base.dto.common.ledger.LedgerDisposeBillDto;
import com.zjuee.base.model.common.ledger.LedgerDisposeBill;
import com.zjuee.service.LedgerDisposeBillService;
import com.zjuee.web.ledger.mapper.LedgerDisposeBillMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class LedgerDisposeBillServiceImpl implements LedgerDisposeBillService {

    @Resource
    private LedgerDisposeBillMapper ledgerDisposeBillMapper;
    @Resource
    private TranslateData translateData;

    @Override
    public Long add(LedgerDisposeBill type) {
        int insert = ledgerDisposeBillMapper.insert(type);
        return Long.parseLong(String.valueOf(insert));
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(LedgerDisposeBill type) {
        return false;
    }

    @Override
    public LedgerDisposeBill selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<LedgerDisposeBill> list(LedgerDisposeBillDto dto) {
        Page<LedgerDisposeBill> ledgerDisposeBillPage = ledgerDisposeBillMapper.findByCustomPager(dto.getPage().convert(), dto);
       translateData.translateData(ledgerDisposeBillPage.getRecords(),
                "LedgerDisposeBillListHandle",
                String.valueOf(DictionaryType.WL_FROM.getValue()),
//                String.valueOf(DictionaryType.WL_DEVICE_FROM.getValue()),
               String.valueOf(DictionaryType.WLD_MARK.getValue()),
               String.valueOf(DictionaryType.PB_WASTE_TYPE.getValue()),
               String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue()),
               String.valueOf(DictionaryType.WL_TRANS_TYPE.getValue())

        );
        return ledgerDisposeBillPage;
    }

    @Override
    public void exportBillExcel(LedgerDisposeBill ledgerDisposeBill) {
        List<LedgerDisposeBill> ledgerDisposeBills = ledgerDisposeBillMapper.selectList(ledgerDisposeBill);
        List<Map<String, Object>> dataMap = (List<Map<String, Object>>) translateData.translateData(ledgerDisposeBills,
                "LedgerDisposeBillListHandle",
                String.valueOf(DictionaryType.WL_FROM.getValue()),
//                String.valueOf(DictionaryType.WL_DEVICE_FROM.getValue()),
                String.valueOf(DictionaryType.WLD_MARK.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_TYPE.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue())
        );
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        String[] key = {"id", "wldbModelStr", "wldbTypeStr", "wldbMarkStr", "wldbBcode", "wldbScode", "wldbFromStr","wldbEndFromStr", "wldbShift", "wldbDispose", "wldbStock","wldgCreateTime", "wldgModifiedTime"};
        String[] str = {"序号", "废物大类别", "废物小类别", "台账类型", "废物大类", "废物小类","台账来源", "台账终端来源","转入量(吨)", "处置量(吨)",  "当前库存量(吨)", "创建时间", "修改时间"};

        List<ExportExcel> exportExcelList = new ArrayList<ExportExcel>();
        ExportExcel e1 = new ExportExcel("处置台账明细列表", "处置台账明细列表", key, str, dataMap);
        exportExcelList.add(e1);
        ExcelUtil.download("处置台账明细列表", exportExcelList, request, response);
    }

    @Override
    public List<LedgerDisposeBill> insertBatch(ArrayList<LedgerDisposeBill> insertLedgerBill) {
        ledgerDisposeBillMapper.insertBatch(insertLedgerBill);
        return insertLedgerBill;
    }

    @Override
    public LambdaQueryWrapper<LedgerDisposeBill> fuzzyQuery(LedgerDisposeBillDto dto) {
        return null;
    }
}
